# -*- coding: utf-8 -*-
import requests
import base64
import time
import urllib3
from cairosvg import svg2png
import cv2
from lxml import etree
import os
from selenium import webdriver
import re
import json

urllib3.disable_warnings()


def file_name(file_dir):
    L = []
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            if os.path.splitext(file)[1] == '.html':
                L.append(os.path.join(root, file))
    return L


def get_img():
    headers = {
        'Connection': 'keep-alive',
        'Cache-Control': 'max-age=0',
        'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"',
        'sec-ch-ua-mobile': '?0',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': '',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Sec-Fetch-Site': 'none',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-User': '?1',
        'Sec-Fetch-Dest': 'document',
        'Accept-Language': 'zh-CN,zh;q=0.9',
    }

    params = (
        ('timer', '1627266386094'),
    )

    response = requests.get('https://zhanzhang.sogou.com/api/user/generateVerifCode', headers=headers, params=params,
                            verify=False)
    html = etree.HTML(response.text)
    path_l = html.xpath('//svg/path/@d')
    for p in path_l:
        if len(p) < 100:
            continue
        with open(f"./len_code_html/{len(p)}.html", 'a') as f:
            tt = f"""
            <svg xmlns="http://www.w3.org/2000/svg" width="80" height="32" viewBox="0,0,80,32">
                <path xmlns="http://www.w3.org/2000/svg" d="{p}" >
            </svg>
            """
            f.write(tt + "\n")


def get_num_code(path_):
    driver = webdriver.Chrome()
    driver.get(path_)
    k = input("请输入值：")
    driver.close()
    return k


def get_code_dict():
    all_p = file_name(file_dir=r"E:\project_\27_update_url\sougou\len_code_html")
    for p in all_p:
        a = re.findall(r'len_code_html\\(.*?).html', p)[0]
        k = get_num_code(path_=p)
        print(k)
        with open('t_t.txt', 'a') as f:
            f.write(f"{a}||{k}" + "\n")


def get_dict_two():
    with open('t_t.txt', 'r') as f:
        ll = f.read().split("\n")
    d = {}
    for i in ll:
        l = i.split("||")
        if l[0]:
            if "," in l[1]:
                d[int(l[0])] = l[1].split(",")
            else:
                d[int(l[0])] = list(l[1])
    print(d)


# get_dict_two()

def get_min_xy(data):
    xs = []
    ys = []
    i = 0
    for f in re.findall('\d+\.\d+', data):
        if i % 2:
            ys.append(float(f))
        else:
            xs.append(float(f))
        i = i + 1
    xs.sort()
    ys.sort()
    return [xs[0], ys[0]]


# print(get_min_xy(data="""
# <svg xmlns="http://www.w3.org/2000/svg" width="80" height="32" viewBox="0,0,80,32">
#                 <path xmlns="http://www.w3.org/2000/svg" d="M7.81 8.44L7.72 8.35L7.67 8.30Q10.38 10.11 13.78 10.27L13.85 10.35L13.90 10.40Q17.09 10.40 20.13 9.15L20.12 9.14L20.10 9.12Q20.07 9.66 19.52 11.64L19.57 11.69L19.42 11.54Q17.47 12.28 15.35 12.39L15.35 12.40L15.36 12.40Q13.32 12.59 11.26 12.08L11.20 12.02L11.22 12.04Q11.63 14.54 11.77 17.16L11.75 17.14L11.80 17.19Q13.29 17.40 14.43 17.37L14.37 17.31L14.39 17.33Q15.25 17.32 16.96 17.24L16.98 17.26L16.95 17.23Q17.03 17.80 17.03 18.29L16.95 18.21L16.88 19.09L16.91 19.12Q15.32 19.24 11.90 19.30L11.74 19.14L11.88 19.28Q11.78 22.12 11.38 24.98L11.48 25.08L11.39 25.00Q13.37 24.63 15.32 24.71L15.22 24.60L15.19 24.58Q17.21 24.73 19.06 25.32L19.06 25.32L19.05 25.31Q19.14 25.91 19.33 26.46L19.38 26.51L19.58 27.47L19.69 27.58Q17.41 26.66 14.96 26.66L14.97 26.67L14.83 26.53Q11.38 26.56 8.33 28.16L8.30 28.12L8.36 28.18Q10.06 23.37 9.87 18.28L9.81 18.22L9.85 18.26Q9.68 13.17 7.78 8.41ZM8.04 28.74L8.05 28.75L8.00 28.70Q8.94 28.23 9.49 27.98L9.41 27.91L9.52 28.02Q9.29 28.47 8.89 29.56L8.99 29.66L8.97 29.65Q12.07 28.09 15.68 28.28L15.68 28.28L15.69 28.28Q19.47 28.58 22.32 30.51L22.27 30.46L22.32 30.51Q21.44 28.65 20.95 27.26L21.02 27.33L20.92 27.24Q20.18 26.77 19.48 26.52L19.58 26.63L19.65 26.70Q19.24 25.58 19.13 25.01L19.16 25.04L19.26 25.13Q16.90 24.27 14.64 24.27L14.80 24.43L14.77 24.40Q13.96 24.41 13.15 24.49L13.04 24.38L13.16 24.51Q13.08 23.45 13.11 22.58L13.10 22.56L13.29 20.91L13.21 20.83Q14.47 20.73 15.74 20.75L15.89 20.90L15.77 20.78Q17.14 20.87 18.42 20.98L18.43 20.99L18.38 20.94Q18.37 20.31 18.37 19.63L18.27 19.53L18.40 18.29L17.86 18.38L17.70 18.22Q17.49 18.36 17.16 18.36L17.14 18.34L17.22 18.42Q17.11 17.82 17.22 16.90L17.27 16.95L17.26 16.94Q16.17 17.16 15.33 17.18L15.25 17.11L15.36 17.21Q14.66 17.20 13.36 17.14L13.20 16.99L13.38 17.17Q13.19 16.02 13.05 14.01L13.06 14.02L13.20 14.16Q13.78 14.08 14.48 14.08L14.50 14.10L14.60 14.19Q17.95 14.17 20.59 12.82L20.47 12.70L20.47 12.69Q20.88 11.02 21.51 9.44L21.60 9.54L21.67 9.60Q20.72 10.20 20.12 10.44L20.14 10.46L20.02 10.35Q20.32 9.89 20.65 8.77L20.57 8.69L20.58 8.71Q17.35 10.31 13.87 10.09L13.87 10.10L13.85 10.08Q10.00 9.79 7.26 7.75L7.36 7.85L7.32 7.81Q9.41 12.81 9.63 18.25L9.65 18.27L9.52 18.15Q9.73 23.79 7.93 28.63Z">
#             </path></svg>
# """))
#
# [4.46, 7.7] U
# [7.26, 7.75] E



length_d = {
    1070: ['v'],
    1071: ['v'],
    1072: ['v'],
    1073: ['v'],
    1074: ['v'],
    1082: ['v'],
    1085: ['Y'],
    1086: ['Y'],
    1101: ['Y'],
    1238: ['L'],
    1239: ['L'],
    1240: ['L', 'V'],
    1241: ['V'],
    1242: ['V'],
    1253: ['y'],
    1254: ['y'],
    1255: ['y'],
    1259: ['L'],
    1263: ['V'],
    1274: ['y'],
    1426: ['T'],
    1427: ['T'],
    1428: ['T'],
    1429: ['T'],
    1430: ['T'],
    1452: ['T'],
    1453: ['T'],
    1454: ['T'],
    1455: ['T'],
    1456: ['T'],
    1480: ['r'],
    1481: ['r'],
    1482: ['r'],
    1491: ['r'],
    1514: ['N'],
    1529: ['X'],
    1530: ['X'],
    1531: ['X'],
    1532: ['X'],
    1566: ['N'],
    1567: ['X'],
    1568: ['X'],
    1569: ['X'],
    1570: ['X'],
    1582: ['J'],
    1583: ['J', 'K', 'x'],
    1584: ['K', 'J'],
    1585: ['K', 'J'],
    1586: ['J'],
    1587: ['J'],
    1596: ['J'],
    1597: ['J'],
    1598: ['J'],
    1599: ['J'],
    1600: ['J'],
    1610: ['x'],
    1619: ['k'],
    1620: ['k'],
    1621: ['K'],
    1622: ['K'],
    1623: ['K'],
    1649: ['z'],
    1650: ['z'],
    1651: ['z'],
    1674: ['K'],
    1675: ['K'],
    1676: ['K'],
    1677: ['K'],
    1678: ['K'],
    1694: ['z', 'f'],
    1704: ['F'],
    1705: ['F'],
    1706: ['F'],
    1707: ['F'],
    1708: ['F'],
    1709: ['F'],
    1732: ['F'],
    1733: ['F'],
    1734: ['F'],
    1735: ['F'],
    1780: ['Z'],
    1781: ['Z'],
    1782: ['Z'],
    1783: ['Z'],
    1793: ['A'],
    1808: ['u'],
    1809: ['u'],
    1810: ['u'],
    1811: ['u'],
    1812: ['u'],
    1821: ['A'],
    1827: ['Z'],
    1828: ['Z', 'H'],
    1829: ['Z', 'H'],
    1830: ['Z', 'H'],
    1831: ['H'],
    1838: ['u'],
    1845: ['h'],
    1846: ['h'],
    1847: ['h'],
    1848: ['h'],
    1872: ['h'],
    1887: ['H'],
    1888: ['H'],
    1889: ['H'],
    1925: ['P'],
    1926: ['P'],
    1927: ['P'],
    1928: ['P'],
    1929: ['P'],
    1930: ['P'],
    1931: ['P'],
    1932: ['P'],
    1941: ['D'],
    1942: ['D'],
    1943: ['D'],
    1944: ['D'],
    1945: ['D'],
    1949: ['P'],
    1950: ['P'],
    1951: ['P'],
    1952: ['P'],
    1953: ['P'],
    1954: ['P'],
    1977: ['w'],
    1978: ['w'],
    1980: ['F'],
    1981: ['F'],
    1982: ['F'],
    1983: ['F'],
    1985: ['D'],
    1986: ['D'],
    2018: ['w'],
    2021: ['F'],
    2022: ['F'],
    2038: ['j'],
    2039: ['j'],
    2040: ['j'],
    2061: ['R'],
    2062: ['R'],
    2063: ['R'],
    2064: ['R'],
    2065: ['R'],
    2066: ['R'],
    2067: ['j'],
    2068: ['j'],
    2093: ['R'],
    2094: ['R'],
    2095: ['R'],
    2096: ['R'],
    2097: ['R'],
    2098: ['R'],
    2106: ['4'],
    2107: ['4'],
    2108: ['4'],
    2117: ['C'],
    2118: ['c'],
    2119: ['C'],
    2126: ['4'],
    2127: ['4'],
    2128: ['4'],
    2129: ['4'],
    2165: ['n'],
    2166: ['n'],
    2167: ['n'],
    2168: ['n'],
    2169: ['n'],
    2171: ['C'],
    2173: ['f'],
    2174: ['f'],
    2175: ['f'],
    2176: ['f'],
    2177: ['f', 'M'],
    2178: ['f', 'M'],
    2179: ['f'],
    2192: ['E'],
    2193: ['E'],
    2194: ['E'],
    2195: ['E', 'U'],
    2196: ['E', 'U'],
    2197: ['U'],
    2198: ['n', 'U'],
    2199: ['U'],
    2200: ['U'],
    2201: ['U'],
    2202: ['U'],
    2204: ['d'],
    2205: ['d'],
    2206: ['d'],
    2207: ['d'],
    2220: ['W'],
    2224: ['E'],
    2225: ['E'],
    2226: ['E'],
    2227: ['E'],
    2246: ['M'],
    2250: ['d'],
    2259: ['u'],
    2260: ['u'],
    2285: ['W'],
    2286: ['a'],
    2326: ['b'],
    2327: ['b'],
    2328: ['b'],
    2329: ['b'],
    2332: ['a'],
    2340: ['s'],
    2341: ['s'],
    2342: ['s'],
    2343: ['s'],
    2344: ['s'],
    2365: ['b'],
    2366: ['s'],
    2432: ['p'],
    2433: ['p'],
    2434: ['p'],
    2452: ['e'],
    2453: ['e'],
    2454: ['e'],
    2455: ['e'],
    2456: ['c', 'e'],
    2457: ['c'],
    2464: ['p'],
    2474: ['q'],
    2476: ['2'],
    2477: ['2'],
    2478: ['2'],
    2479: ['2'],
    2480: ['2'],
    2481: ['2'],
    2482: ['2'],
    2483: ['2'],
    2498: ['c'],
    2501: ['e'],
    2512: ['q'],
    2534: ['2'],
    2535: ['2'],
    2536: ['2'],
    2537: ['2'],
    2538: ['2'],
    2539: ['2'],
    2540: ['2'],
    2547: ['5'],
    2548: ['5'],
    2549: ['5'],
    2550: ['5'],
    2551: ['5'],
    2552: ['5'],
    2553: ['5'],
    2567: ['6'],
    2568: ['6'],
    2569: ['6'],
    2570: ['6'],
    2571: ['6'],
    2594: ['5'],
    2595: ['5'],
    2596: ['5'],
    2597: ['5'],
    2598: ['5'],
    2614: ['6'],
    2615: ['6'],
    2616: ['6'],
    2617: ['6'],
    2744: ['9'],
    2745: ['9'],
    2746: ['9'],
    2747: ['9'],
    2748: ['9'],
    2749: ['9'],
    2786: ['9'],
    2787: ['9'],
    2788: ['9'],
    2789: ['9'],
    2790: ['9'],
    2791: ['9'],
    2978: ['S'],
    2979: ['S'],
    2980: ['S'],
    2981: ['S'],
    2982: ['S'],
    2983: ['S'],
    3015: ['B'],
    3016: ['B'],
    3017: ['B'],
    3018: ['B'],
    3019: ['B'],
    3029: ['S'],
    3030: ['S'],
    3031: ['S'],
    3032: ['S'],
    3044: ['B'],
    3045: ['B'],
    3046: ['B'],
    3047: ['B'],
    3093: ['g'],
    3094: ['g'],
    3095: ['g'],
    3096: ['g'],
    3157: ['Q'],
    3158: ['Q'],
    3159: ['Q'],
    3160: ['g', 'Q'],
    3190: ['G'],
    3191: ['G'],
    3192: ['G'],
    3193: ['G'],
    3233: ['Q'],
    3234: ['Q'],
    3235: ['Q'],
    3236: ['Q'],
    3237: ['Q'],
    3252: ['G'],
    3253: ['G'],
    3254: ['G'],
    3255: ['G'],
    3256: ['G'],
    3354: ['8'],
    3355: ['8'],
    3356: ['8'],
    3357: ['8'],
    3358: ['8'],
    3359: ['8'],
    3360: ['8'],
    3404: ['8'],
    3405: ['8'],
    3406: ['8'],
    3407: ['8'],
    3408: ['8'],
    3409: ['8'],
    3410: ['8'],
    3411: ['8'],
    3584: ['m'],
    3585: ['m'],
    3586: ['m'],
    3663: ['m'],
    3797: ['3'],
    3798: ['3'],
    3799: ['3'],
    3800: ['3'],
    3801: ['3'],
    3802: ['3'],
    3803: ['3'],
    3804: ['3'],
    3864: ['3'],
    3865: ['3'],
    3866: ['3'],
    3867: ['3'],
    3868: ['3'],
    3869: ['3']
}


def get_same_length(data):
    if len(data) == 1240:
        if get_min_xy(data=data)[1] > 10:
            return "Q"
        else:
            return "G"
    elif len(data) == 1583:
        if get_min_xy(data=data)[1] > 15:
            return "x"
        elif get_min_xy(data=data)[0] > 8:
            return "j"
        else:
            return "k"
    elif len(data) == 1584:
        if get_min_xy(data=data)[1] > 8:
            return "j"
        else:
            return "k"
    elif len(data) == 1585:
        if get_min_xy(data=data)[1] > 8:
            return "J"
        else:
            return "k"
    elif len(data) in (1828, 1829, 1830):
        if get_min_xy(data=data)[0] > 5:
            return "Z"
        else:
            return "H"
    elif len(data) == 1694:
        if get_min_xy(data=data)[1] > 12:
            return "Z"
        else:
            return "f"
    elif len(data) in (2177, 2178):
        if get_min_xy(data=data)[0] > 9:
            return "f"
        else:
            return "M"
    elif len(data) in (2456,):
        if get_min_xy(data=data)[0] > 8:
            return "c"
        else:
            return "e"
    elif len(data) in (2456, 3160):
        if get_min_xy(data=data)[0] > 50:
            return "g"
        else:
            return "Q"
    elif len(data) in (2198,):
        if get_min_xy(data=data)[0] > 40:
            return "n"
        else:
            return "U"

    elif len(data) in (2195, 2196):
        if get_min_xy(data=data)[0] > 5:
            return "E"
        else:
            return "U"


def get_code_html():
    headers = {
        'Connection': 'keep-alive',
        'Cache-Control': 'max-age=0',
        'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"',
        'sec-ch-ua-mobile': '?0',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': '',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,'
                  '*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Sec-Fetch-Site': 'none',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-User': '?1',
        'Sec-Fetch-Dest': 'document',
        'Accept-Language': 'zh-CN,zh;q=0.9',
    }

    params = (
        ('timer', '1627266386094'),
    )

    response = requests.get('https://zhanzhang.sogou.com/api/user/generateVerifCode', headers=headers, params=params,
                            verify=False)
    with open("html.html", 'w') as f:
        f.write(response.text + "\n")
    return response.text


def get_code(html_text):
    html = etree.HTML(html_text)
    path_l = html.xpath('//svg/path/@d')
    path_l = sorted(path_l, key=lambda x: float(x[1:10].split(" ")[0]))
    s = ""
    for p in path_l:
        if len(p) < 100:
            continue
        if len(length_d[len(p)]) > 1:
            try:
                num = get_same_length(p)
                s += num
            except Exception:
                print(len(p))
                return False
        else:
            num = length_d[len(p)]
            s += num[0]
    return s


# #
# for i in range(10):
#     html_ = get_code_html()
#     print(get_code(html_text=html_))
#     time.sleep(1)
